package com.adtech.adms.system.web.action;

import java.util.ArrayList;
import java.util.List;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.ErrorCode;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.system.pojo.Topo;
import com.adtech.adms.system.pojo.User;
import com.adtech.adms.system.service.TopoService;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.web.action.AjaxResult;
import com.adtech.core.web.action.BaseAction;

public class TopoAction extends BaseAction{
	private TopoService topoService;
	
	
	
	private List<TreeNode<Topo>> nodes;
	private Long id;
	private String selectIds;
	private Long slectId;
	private boolean allLevel = true;
	private Topo topo;
	@Override
	public String execute() {
		return SUCCESS;
	}
	
	public String topoSelet(){
		List<Topo> topos = new ArrayList<Topo>();
		if (selectIds != null && selectIds.length() != 0) {
			String[] ids = selectIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if (!ids[i].equals("")) {
					Topo topo = topoService.getTopoById(new Long(ids[i]));
					topos.add(topo);
				}
			}
		}
		session.put(SessionKey.SESSION_KEY_SELECT_TOPOS, topos);
		
		return SUCCESS;
	}
	public String getTopoTree() {
		if (id == null) {
			nodes = topoService.getTopoTree(allLevel);
		} else {
			nodes = topoService.getTopoTree(id, allLevel);
		}
		return SUCCESS;
	}
	
	public String getTopoDetail() {
		if (id != null) {
			topo = topoService.getTopoById(id);
		}
		return SUCCESS;
	}
	public String save() throws ServiceException {
		
		topoService.save(slectId,topo,(User) session.get(SessionKey.SESSION_KEY_USER));
		setAjaxResult(AjaxResult.success());
		nodes = topoService.getTopoTree(allLevel);
		return SUCCESS;
	}
	
	public String delete() throws ServiceException {
		if (id != null) {
			topoService.delete(id,(User)session.get(SessionKey.SESSION_KEY_USER));
		}
		setAjaxResult(AjaxResult.success());
		return SUCCESS;
	}
	
	public String getCheck() {
		return SUCCESS;
	}

	
	
//	public String getFeatureTree() {
//		if (id == null) {
//			nodes = topoService.getTopoTree(allLevel);
//		} else {
//			nodes = topoService.getTopoTree(id, allLevel);
//		}
//		return SUCCESS;
//	}

	public TopoService getTopoService() {
		return topoService;
	}

	public void setTopoService(TopoService topoService) {
		this.topoService = topoService;
	}

	public List<TreeNode<Topo>> getNodes() {
		return nodes;
	}

	public void setNodes(List<TreeNode<Topo>> nodes) {
		this.nodes = nodes;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public boolean isAllLevel() {
		return allLevel;
	}

	public void setAllLevel(boolean allLevel) {
		this.allLevel = allLevel;
	}

	public Topo getTopo() {
		return topo;
	}

	public void setTopo(Topo topo) {
		this.topo = topo;
	}

	public Long getSlectId() {
		return slectId;
	}

	public void setSlectId(Long slectId) {
		this.slectId = slectId;
	}

	public String getSelectIds() {
		return selectIds;
	}

	public void setSelectIds(String selectIds) {
		this.selectIds = selectIds;
	}
	
	
	
}
